package com.ftaillefer.berkeydbexplore;

import com.sleepycat.je.*;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;
import java.io.File;

/**
 * Hello world!
 *
 */
public class App 
{
   public EnvironmentConfig config = new EnvironmentConfig();
    static File dbDir = new File("/home/ftaillefer/Berkeley-DB/test1");
    static Environment dbEnv;
    static EntityStore dbStore;

    
    public static void main( String[] args ) throws Exception
    {
        setUp();
         testStoreAndRetrieveOne();
    }
    
     protected static void setUp() throws Exception {
        if (!dbDir.exists()) {
            dbDir.mkdir();
        }

        EnvironmentConfig config = new EnvironmentConfig();
        config.setAllowCreate(true);
        dbEnv = new Environment(dbDir, config);

        StoreConfig storeConfig = new StoreConfig();
        storeConfig.setAllowCreate(true);
        dbStore = new EntityStore(dbEnv, "EntityStore", storeConfig);

    }
    
   public static void testStoreAndRetrieveOne() throws DatabaseException
  {
    BlogPost newPosting = 
      new BlogPost("The Vietnam of Computer Science2");
  
    PrimaryIndex<String,BlogPost> primaryIndex =
      dbStore.getPrimaryIndex(String.class, BlogPost.class);
    primaryIndex.put(newPosting);
      BlogPost vietnam = 
      primaryIndex.get("The Vietnam of Computer Science2");

  }

}
